﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditUnitTrustGuaranteeModel
    {
        public UnitTrustTransactionScreenData ScreenData { get; set; }
        public UnitTrustHeader Header { get; set; }
        public UnitTrustTransaction Transaction { get; set; }
        public EditUnitTrustGuaranteeModel()
        {
            Init();
        }
        void Init()
        {
            if (null == Header)
                Header = new UnitTrustHeader();
            if (null == Transaction)
                Transaction = new UnitTrustTransaction();
            var defaultItem = WebUtil.SelectListSelectDefault();
            TransactionTypeList = new List<SelectListItem>();
            ObligationList = new BindingList<SelectListItem> { defaultItem };
            ObligationNoList = new BindingList<SelectListItem> { defaultItem };
        }

        public static EditUnitTrustGuaranteeModel BuildModel(UnitTrustTransactionScreenData data)
        {
            var model = new EditUnitTrustGuaranteeModel { ScreenData = data };
            if (data.HeaderDTO.Header != null)
                model.Header = data.HeaderDTO.Header;
            if (data.Transaction != null)
                model.Transaction = data.Transaction;

            model.TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Guarantee", Value = "Guarantee" });
            //model.TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Subscription", Value = "Subscription" });
            model.SelectedTransactionType = "Guarantee";

            foreach (var item in data.Obligations)
            {
                model.ObligationList.Add(new SelectListItem() { Text = item.Name, Value = item.ID.ToString() });
            }
            foreach (var item in data.ObligationNOs)
            {
                model.ObligationNoList.Add(new SelectListItem() { Text = item.Name, Value = item.ID.ToString() });
            }

            model.HeaderID = model.Header.ID;
            model.TransactionID = model.Transaction.ID;

            return model;
        }

        #region dropdownlist data
        //TransactionType
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedTransactionType
        {
            get { return Transaction.TransactionType; }
            set { Transaction.TransactionType = value; }
        }
        public IList<SelectListItem> TransactionTypeList { get; set; }

        [Required(ErrorMessage = "โปรดระบุ")]
        public String SelectedObligation
        {
            get { return Transaction.Obligation; }
            set { Transaction.Obligation = value; }
        }
        public IList<SelectListItem> ObligationList { set; get; }

        [Required(ErrorMessage = "โปรดระบุ")]
        public String SelectedObligationNo
        {
            get { return Transaction.ObligationNO; }
            set { Transaction.ObligationNO = value; }
        }
        public IList<SelectListItem> ObligationNoList { set; get; }
        #endregion

        #region adapter data
        public int HeaderID
        {
            get
            {
                return Transaction.UnitTrustTransactionHeaderID;
            }

            set
            {
                Transaction.UnitTrustTransactionHeaderID = value;
            }
        }
        public int TransactionID
        {
            get
            {
                return Transaction.ID;
            }

            set
            {
                Transaction.ID = value;
            }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public string RefNo
        {
            get { return Transaction.RefNO; }
            set { Transaction.RefNO = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? Unit
        {
            get { return Transaction.Unit; }
            set { Transaction.Unit = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:dd'/'MM'/'yyyy}")]
        [Display(Name = "Start Date")]
        public DateTime? StartDate
        {
            get { return Transaction.GuaranteeStartDate; }
            set { Transaction.GuaranteeStartDate = value; }
        }


        [Required(ErrorMessage = "โปรดระบุ")]
        [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:dd'/'MM'/'yyyy}")]
        [Display(Name = "End Date")]
        public DateTime? EndDate
        {
            get { return Transaction.GuaranteeEndDate; }
            set { Transaction.GuaranteeEndDate = value; }
        }
        #endregion

        public String SubmitBtn { get; set; }
        public String CancelBtn { get; set; }
        public bool UpdateMode { get; set; }

    }
}